
#include <GLFW/glfw3.h>
#include "time.h"

#include <glog/logging.h>
namespace csugl
{
    // glfw lowp time

    void Time<float>::init()
    {
        _currentTime = glfwGetTime();
        _deltaTime = 1.0 / 60.0;
    }

    void Time<float>::update()
    {
        float t = glfwGetTime();
        _deltaTime = t - _currentTime;
        _currentTime = t;
    }

    float Time<float>::currentTime() { return glfwGetTime(); }

    float Time<float>::_currentTime = glfwGetTime();

    float Time<float>::_deltaTime = 1.0 / 60;

    // highp time

    void Time<double>::init()
    {
        _currentTime = std::chrono::steady_clock::now();
        _deltaTime = 1. / 60.;
    }

    void Time<double>::update()
    {
        auto t = std::chrono::steady_clock::now();
        _deltaTime = std::chrono::duration<double>(t - _currentTime).count();
        _currentTime = t;
    }

    double Time<double>::currentTime()
    {
        return std::chrono::duration<double>(std::chrono::steady_clock::now() - _startTime).count();
    }

    std::chrono::steady_clock::time_point Time<double>::_startTime = std::chrono::steady_clock::now();

    std::chrono::steady_clock::time_point Time<double>::_currentTime = std::chrono::steady_clock::now();

    double Time<double>::_deltaTime = 1.0 / 60;

    Timer<float>::~Timer()
    {
        LOG(INFO) << "Timer: [" << name << "] cost: " << std::chrono::duration<float>(std::chrono::steady_clock::now() - start_time).count() * 1000.0f << "ms";
    }
}
